﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Macad.Core;
using Macad.Core.Topology;
using Macad.Interaction;
using Macad.Occt;
using SpCad;
using SpCad.Tools;
using SpCad.Utils;

namespace SPCAM.Plugin.Cam.Tools
{
    public class MoveToZero : Tool
    {
        public enum MoveTope
        {
            max, mini, setpont, Zore
        }
        [Flags]
        public enum LockAxis
        {
            None,X,Y,Z
        }
        private MoveTope _MoveTope;
        InteractiveEntity _body;
        SelectAction _select;
        public MoveToZero(InteractiveEntity body, MoveTope mod)
        {
            _MoveTope = mod;
            
            _body = body;
         
        }
        protected override bool OnStart()
        {
            if (SpServer.Instance.SelectBody == null)
            {
                SpServer.Instance.SetHintMessage("必须先选择一个实体");
                return false;
            }
            switch (_MoveTope)
            {
                case MoveTope.max:
                    SetMini();
                    break;
                case MoveTope.mini:
                    SetMax();
                    break;
                case MoveTope.setpont:
                    Setpont();
                    break;
                case MoveTope.Zore:
                    SetZore();
                    break;
                default:
                    break;
            }
           
            return base.OnStart();
        }
        protected override void OnStop()
        {
          
            if (_select != null)
            {
                _select?.Stop();
                _select.Finished -= Select_Finished;
            }
           
            base.OnStop();
        }
        private void SetMini()
        {
            MoveTo(new Vec(0, 0, 0));
            var loc = SpServer.Instance.WorkingPlane.Location;
            var BoundBox = GetBoundingBox();
            var BoxMini = BoundBox.CornerMin();
            Vec tranvec = new Vec(BoxMini, loc);
            MoveTo(tranvec);

        }
        private void SetMax()
        {
            MoveTo(new Vec(0, 0, 0));
            var loc = SpServer.Instance.WorkingPlane.Location;
            var BoundBox = GetBoundingBox();
            var BoxMini = BoundBox.CornerMax();
            Vec tranvec = new Vec(BoxMini, loc);
            MoveTo(tranvec);
        }
        private void Setpont()
        {
            SpServer.Instance.SetHintMessage("请手动选择一个订单");
            var toposhape = _body.GetTransformedBRep();
            var vert = toposhape.Vertices();
            _select = new SelectAction(SubshapeTypes.Vertex, _body);
            if (!StartAction(_select)) 
            {
                return;
            }
            foreach (var item in vert)
            {
                _select.AddSubshape(item,Trsf.Identity, false);
            }

            _select.Finished += Select_Finished;
        }
        private void SetZore()
        {
            MoveTo(new Vec(0, 0, 0));
        }
        private void Select_Finished(SelectAction sender, SelectAction.EventArgs args)
        {


            var tshap = args.SelectedSubshape;
            if (tshap != null)
            {
                if (tshap is TopoDS_Vertex)
                {
                  //  MoveTo(new Vec(0, 0, 0));
                    var loc = SpServer.Instance.WorkingPlane.Location;
                    var brenpnt = BRep_Tool.Pnt(tshap?.ToVertex());
                    var Tpart = (_body as Body)?.GetTransformation().TranslationPart();
                    if (Tpart != null)
                    {
                        var pr = Tpart.Value;
                        Pnt prpnt = new Pnt(pr.X, pr.Y, pr.Z);
                        Vec tranveca = new(loc,prpnt);
                        Vec Tmov = new Vec(brenpnt,new Pnt(tranveca.X, tranveca.Y, tranveca.Z));
                        MoveTo(Tmov);
                    }
 
                }
            }
            sender.Stop();
        }

        private Bnd_Box GetBoundingBox()
        {
            TopoDS_Shape ShapeBrep = _body.GetTransformedBRep();
            AIS_Shape aisShape = new AIS_Shape(ShapeBrep);
            var BoundBox = ShapeBrep.BoundingBox();
            return BoundBox;
        }
        private void MoveTo(Vec tranvec)
        {
            var  body = (Body)_body;
            if (tranvec.Z < 0)
            {
                tranvec.Z = 0;
            }
            body.Position = new Pnt(tranvec.X, tranvec.Y, tranvec.Z);
            SpServer.Instance.Commit();
          
        }
    }
}
